<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * banner
     */
    public function banner()
    {
        $list = Db::name('banner')->field('id,title,image')->where('status','normal')->select();
        if(!$list)
            $this->error('暂无数据');
        $this->success('',$list);
    }

    /**
     * 团建视频
     */
    public function teamBuildingVideo()
    {
        //$list = Db::name('team_building_video')->field('id,title,type,video')->select();

        $list = Db::field('id,title,type,video')
            ->name('team_building_video')
            ->where(['type'=>1,'is_top'=>1])
            ->union(['SELECT id,title,type,video FROM ed_team_building_video where type=2 and is_top=1 limit 1','SELECT id,title,type,video FROM ed_team_building_video where type=3 and is_top=1 limit 1','SELECT id,title,type,video FROM ed_team_building_video where type=4 and is_top=1 limit 1'])
            ->select();

        if(!$list)
            $this->error('暂无数据');

        $arr = [1=>'忆群人团建',2=>'铁血训练营',3=>'海速体育',4=>'院校素拓教育'];
        foreach($list as $k=>$v)
        {
            $list[$k]['typeName'] = $arr[$v['type']];
        }
        $this->success('',$list);
    }

    /**
     * 团建日报
     */
    public function teamBuildingDaily()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        $list = Db::name('team_building_daily')->limit(6)->select();
        if(!$list)
            $this->error('暂无数据');
        foreach ($list as $k=>$v)
        {
            $list[$k]['date'] = date('Y年m月',$v['createtime']);
            $list[$k]['time'] = date('d',$v['createtime']);
        }
        $this->success('',$list);
    }

    /**
     * 团建日报详情
     */
    public function teamBuildingDailyDetail()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');

        if(!$id=input('get.id'))
            $this->error('缺少参数');

        $info = Db::name('team_building_daily')->where('id',$id)->find();
        if(!$info)
            $this->error('找不到记录');
        Db::name('team_building_daily')->where('id', $id)->setInc('look');
        $info['date'] = date('Y-m-d',$info['createtime']);
        $this->success('',$info);
    }

    /**
     * 铁血新闻
     */
    public function news()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');

        $list = [];
        $tx_new = Db::name('tx_new')->field("id,title,FROM_UNIXTIME(createtime,'%m-%d') as time")->order('id','desc')->limit(5)->select();
        $industry_information = Db::name('industry_information')->field("id,title,FROM_UNIXTIME(createtime,'%m-%d') as time")->order('id','desc')->limit(5)->select();
        $classroom = Db::name('classroom')->field("id,title,FROM_UNIXTIME(createtime,'%m-%d') as time")->order('id','desc')->limit(5)->select();
        $list['tx_new'] = $tx_new;
        $list['industry_information'] = $industry_information;
        $list['classroom'] = $classroom;
        if(!$list)
            $this->error('暂无数据');

        $this->success('',$list);
    }

    /**
     * 新闻详情
     */
    public function news_detail()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        if(!$id = input('get.id'))
        {
            $this->error('缺少参数');
        }

        if(!$type = input('get.type'))
        {
            $this->error('缺少参数');
        }
        $table_name='tx_new';
        switch ($type)
        {
            case 1:
                $table_name='tx_new';
                break;
            case 2:
                $table_name='industry_information';
                break;
            case 3:
                $table_name='classroom';
                break;
        }
       /* $arr = ['tx_new','industry_information','classroom'];
        if(!in_array($table_name,$arr))
        {
            $this->error('参数错误');
        }*/
       $info = Db::name($table_name)->field("*,FROM_UNIXTIME(createtime,'%Y-%m-%d') as time")->where('id',$id)->find();
        if(!$info)
            $this->error('暂无数据');

        $this->success('',$info);

    }

    /**
     * 团建优选
     */
    public function programme()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');

        $where = [];
        if($course_type_purpose_id = input('get.course_type_purpose_id'))
        {
            $where['a.course_type_purpose_id'] = $course_type_purpose_id;
        }

        $list = [];
        $list1 = Db::name('programme')->alias('a')->join('admin b','a.principal_id=b.id')->where('a.course_type_id',2)->where($where)->field('a.id,a.title,a.image,a.look,a.likes,a.collect,b.nickname,b.avatar')->limit(6)->select();
        $list2 = Db::name('programme')->alias('a')->join('admin b','a.principal_id=b.id')->where('a.course_type_id',1)->where($where)->field('a.id,a.title,a.image,a.look,a.likes,a.collect,b.nickname,b.avatar')->limit(6)->select();
        $list3 = Db::name('programme')->alias('a')->join('admin b','a.principal_id=b.id')->where('a.course_type_id',3)->where($where)->field('a.id,a.title,a.image,a.look,a.likes,a.collect,b.nickname,b.avatar')->limit(6)->select();

        $list['data_1'] = $list1;
        $list['data_2'] = $list2;
        $list['data_3'] = $list3;
        if(!$list)
            $this->error('暂无数据');
        $this->success('',$list);
    }

    public function programme_search()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        if(!$course_type_purpose_id = input('get.course_type_purpose_id'))
        {
            $this->error('缺少参数');
        }
        if(!$type = input('get.type'))
        {
            $this->error('缺少参数');
        }

        $list =  Db::name('programme')->alias('a')->join('admin b','a.principal_id=b.id')->where(['a.course_type_id'=>$type])->whereRaw(" FIND_IN_SET('".$course_type_purpose_id."',course_type_purpose_id)")->field('a.id,a.title,a.image,a.look,a.likes,a.collect,b.nickname,b.avatar,a.course_type_id as type,a.course_type_purpose_id')->limit(6)->select();

        if(!$list)
            $this->error('暂无数据');

        foreach($list as $k=>&$v)
        {
            $types = Db::name('course_type_purpose')->where(['id'=>['in',$v['course_type_purpose_id']]])->field('title')->select();
            $v['purpose'] = $types;
        }
        $this->success('',$list);

    }

    /**
     * 首页轮播图
     */
    public function programme_lunbo()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        $list = Db::name('rm_programme')->where('is_home',1)->field('id,title,image,price,place_departure')->limit(5)->select();//->orderRaw('rand()')


        if(!$list)
            $this->error('暂无数据');
       /* foreach($list as $k=>&$v)
        {
            $type_purpose = Db::name('course_type_purpose')->field('title')->where(['id'=>['in',$v['course_type_purpose_id']]])->select();
            $v['type_purpose'] = $type_purpose;
        }*/
        $this->success('',$list);
    }

    /**
     * 分类
     */
    public function classification()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        $list = Db::name('course_type_purpose')->field('id,title,is_home')->select();
        if(!$list)
            $this->error('暂无数据');
        $this->success('',$list);
    }

    /**
     * 场地资源
     */
    public function field()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        $list = Db::name('field')->field('id,title,image')->where('status',1)->limit(6)->select();
        if(!$list)
            $this->error('暂无数据');
        $this->success('',$list);
    }


    /**
     * 师资团队
     */
    public function teacher()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        $list = Db::name('teacher')->field('id,name,head_avatar,type,remark')->where(['status'=>1,'is_home'=>1])->limit(4)->select();
        if(!$list)
            $this->error('暂无数据');
        $this->success('',$list);
    }
}
